home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / icmp.c < prev    next >
C/C++ Source or Header  |  1992-01-08  |  7KB  |  266 lines

  1. /* @(#) $Header: icmp.c,v 1.7 92/01/08 13:45:11 deyke Exp $ */
  2.  
  3. /* Internet Control Message Protocol (ICMP)
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include <stdio.h>
  7. #include "global.h"
  8. #include "mbuf.h"
  9. #include "iface.h"
  10. #include "ip.h"
  11. #include "icmp.h"
  12. #include "netuser.h"
  13.  
  14. struct mib_entry Icmp_mib[] = {
  15.     "",                     0,
  16.     "icmpInMsgs",           0,
  17.     "icmpInErrors",         0,
  18.     "icmpInDestUnreachs",   0,
  19.     "icmpInTimeExcds",      0,
  20.     "icmpInParmProbs",      0,
  21.     "icmpInSrcQuenchs",     0,
  22.     "icmpInRedirects",      0,
  23.     "icmpInEchos",          0,
  24.     "icmpInEchoReps",       0,
  25.     "icmpInTimestamps",     0,
  26.     "icmpInTimestampReps",  0,
  27.     "icmpInAddrMasks",      0,
  28.     "icmpInAddrMaskReps",   0,
  29.     "icmpOutMsgs",          0,
  30.     "icmpOutErrors",        0,
  31.     "icmpOutDestUnreachs",  0,
  32.     "icmpOutTimeExcds",     0,
  33.     "icmpOutParmProbs",     0,
  34.     "icmpOutSrcQuenchs",    0,
  35.     "icmpOutRedirects",     0,
  36.     "icmpOutEchos",         0,
  37.     "icmpOutEchoReps",      0,
  38.     "icmpOutTimestamps",    0,
  39.     "icmpOutTimestampReps", 0,
  40.     "icmpOutAddrMasks",     0,
  41.     "icmpOutAddrMaskReps",  0,
  42. };
  43.  
  44. /* Process an incoming ICMP packet */
  45. void
  46. icmp_input(iface,ip,bp,rxbroadcast)
  47. struct iface *iface;    /* Incoming interface (ignored) */
  48. struct ip *ip;          /* Pointer to decoded IP header structure */
  49. struct mbuf *bp;        /* Pointer to ICMP message */
  50. int rxbroadcast;
  51. {
  52.     struct icmplink *ipp;
  53.     struct mbuf *tbp;
  54.     struct icmp icmp;       /* ICMP header */
  55.     struct ip oip;          /* Offending datagram header */
  56.     int16 type;             /* Type of ICMP message */
  57.     int16 length;
  58.  
  59.     icmpInMsgs++;
  60.     if(rxbroadcast){
  61.         /* Broadcast ICMP packets are to be IGNORED !! */
  62.         icmpInErrors++;
  63.         free_p(bp);
  64.         return;
  65.     }
  66.     length = ip->length - IPLEN - ip->optlen;
  67.     if(cksum(NULLHEADER,bp,length) != 0){
  68.         /* Bad ICMP checksum; discard */
  69.         icmpInErrors++;
  70.         free_p(bp);
  71.         return;
  72.     }
  73.     ntohicmp(&icmp,&bp);
  74.  
  75.     /* Process the message. Some messages are passed up to the protocol
  76.      * module for handling, others are handled here.
  77.      */
  78.     type = icmp.type;
  79.  
  80.     switch(uchar(type)){
  81.     case ICMP_TIME_EXCEED:  /* Time-to-live Exceeded */
  82.     case ICMP_DEST_UNREACH: /* Destination Unreachable */
  83.     case ICMP_QUENCH:       /* Source Quench */
  84.         switch(uchar(type)){
  85.         case ICMP_TIME_EXCEED:  /* Time-to-live Exceeded */
  86.             icmpInTimeExcds++;
  87.             break;
  88.         case ICMP_DEST_UNREACH: /* Destination Unreachable */
  89.             icmpInDestUnreachs++;
  90.             break;
  91.         case ICMP_QUENCH:       /* Source Quench */
  92.             icmpInSrcQuenchs++;
  93.             break;
  94.         }
  95.         ntohip(&oip,&bp);       /* Extract offending IP header */
  96.         if(Icmp_trace){
  97.             printf("ICMP from %s:",inet_ntoa(ip->source));
  98.             printf(" dest %s %s",inet_ntoa(oip.dest),
  99.              smsg(Icmptypes,ICMP_TYPES,uchar(type)));
  100.             switch(uchar(type)){
  101.             case ICMP_TIME_EXCEED:
  102.                 printf(" %s\n",
  103.                  smsg(Exceed,NEXCEED,uchar(icmp.code)));
  104.                 break;
  105.             case ICMP_DEST_UNREACH:
  106.                 printf(" %s\n",
  107.                  smsg(Unreach,NUNREACH,uchar(icmp.code)));
  108.                 break;
  109.             default:
  110.                 printf(" %u\n",uchar(icmp.code));
  111.                 break;
  112.             }
  113.         }
  114.         for(ipp = Icmplink;ipp->funct != NULL;ipp++)
  115.             if(ipp->proto == oip.protocol)
  116.                 break;
  117.         if(ipp->funct != NULL){
  118.             (*ipp->funct)(ip->source,oip.source,oip.dest,icmp.type,
  119.              icmp.code,&bp);
  120.         }
  121.         break;
  122.     case ICMP_ECHO:         /* Echo Request */
  123.         /* Change type to ECHO_REPLY, recompute checksum,
  124.          * and return datagram.
  125.          */
  126.         icmpInEchos++;
  127.         icmp.type = ICMP_ECHO_REPLY;
  128.         if((tbp = htonicmp(&icmp,bp)) == NULLBUF){
  129.             free_p(bp);
  130.             return;
  131.         }
  132.         icmpOutEchoReps++;
  133.         ip_send(ip->dest,ip->source,ICMP_PTCL,ip->tos,0,tbp,length,0,0);
  134.         return;
  135.     case ICMP_REDIRECT:     /* Redirect */
  136.         icmpInRedirects++;
  137.         break;
  138.     case ICMP_PARAM_PROB:   /* Parameter Problem */
  139.         icmpInParmProbs++;
  140.         break;
  141.     case ICMP_ECHO_REPLY:   /* Echo Reply */
  142.         icmpInEchoReps++;
  143.         echo_proc(ip->source,ip->dest,&icmp,bp);
  144.         bp = NULLBUF;   /* so it won't get freed */
  145.         break;
  146.     case ICMP_TIMESTAMP:    /* Timestamp */
  147.         icmpInTimestamps++;
  148.         break;
  149.     case ICMP_TIME_REPLY:   /* Timestamp Reply */
  150.         icmpInTimestampReps++;
  151.         break;
  152.     case ICMP_INFO_RQST:    /* Information Request */
  153.         break;
  154.     case ICMP_INFO_REPLY:   /* Information Reply */
  155.         break;
  156.     }
  157.     free_p(bp);
  158. }
  159. /* Return an ICMP response to the sender of a datagram.
  160.  * Unlike most routines, the callER frees the mbuf.
  161.  */
  162. int
  163. icmp_output(ip,data,type,code,args)
  164. struct ip *ip;          /* Header of offending datagram */
  165. struct mbuf *data;      /* Data portion of datagram */
  166. char type,code;         /* Codes to send */
  167. union icmp_args *args;
  168. {
  169.     struct mbuf *bp = NULLBUF;
  170.     struct icmp icmp;       /* ICMP protocol header */
  171.     int16 dlen;             /* Length of data portion of offending pkt */
  172.     int16 length;           /* Total length of reply */
  173.  
  174.     if(ip == NULLIP)
  175.         return -1;
  176.     if(uchar(ip->protocol) == ICMP_PTCL){
  177.         /* Peek at type field of ICMP header to see if it's safe to
  178.          * return an ICMP message
  179.          */
  180.         switch(uchar(data->data[0])){
  181.         case ICMP_ECHO_REPLY:
  182.         case ICMP_ECHO:
  183.         case ICMP_TIMESTAMP:
  184.         case ICMP_TIME_REPLY:
  185.         case ICMP_INFO_RQST:
  186.         case ICMP_INFO_REPLY:
  187.             break;  /* These are all safe */
  188.         default:
  189.             /* Never send an ICMP error message about another
  190.              * ICMP error message!
  191.              */
  192.             return -1;
  193.         }
  194.     }
  195.     /* Compute amount of original datagram to return.
  196.      * We return the original IP header, and up to 8 bytes past that.
  197.      */
  198.     dlen = min(8,len_p(data));
  199.     length = dlen + ICMPLEN + IPLEN + ip->optlen;
  200.     /* Take excerpt from data portion */
  201.     if(data != NULLBUF && dup_p(&bp,data,0,dlen) == 0)
  202.         return -1;      /* The caller will free data */
  203.  
  204.     /* Recreate and tack on offending IP header */
  205.     if((data = htonip(ip,bp,IP_CS_NEW)) == NULLBUF){
  206.         free_p(bp);
  207.         icmpOutErrors++;
  208.         return -1;
  209.     }
  210.     icmp.type = type;
  211.     icmp.code = code;
  212.     icmp.args.unused = 0;
  213.     switch(uchar(icmp.type)){
  214.     case ICMP_PARAM_PROB:
  215.         icmpOutParmProbs++;
  216.         icmp.args.pointer = args->pointer;
  217.         break;
  218.     case ICMP_REDIRECT:
  219.         icmpOutRedirects++;
  220.         icmp.args.address = args->address;
  221.         break;
  222.     case ICMP_ECHO:
  223.         icmpOutEchos++;
  224.         break;
  225.     case ICMP_ECHO_REPLY:
  226.         icmpOutEchoReps++;
  227.         break;
  228.     case ICMP_INFO_RQST:
  229.         break;
  230.     case ICMP_INFO_REPLY:
  231.         break;
  232.     case ICMP_TIMESTAMP:
  233.         icmpOutTimestamps++;
  234.         break;
  235.     case ICMP_TIME_REPLY:
  236.         icmpOutTimestampReps++;
  237.         icmp.args.echo.id = args->echo.id;
  238.         icmp.args.echo.seq = args->echo.seq;
  239.         break;
  240.     case ICMP_ADDR_MASK:
  241.         icmpOutAddrMasks++;
  242.         break;
  243.     case ICMP_ADDR_MASK_REPLY:
  244.         icmpOutAddrMaskReps++;
  245.         break;
  246.     case ICMP_DEST_UNREACH:
  247.         if(icmp.code == ICMP_FRAG_NEEDED)
  248.             icmp.args.mtu = args->mtu;
  249.         icmpOutDestUnreachs++;
  250.         break;
  251.     case ICMP_TIME_EXCEED:
  252.         icmpOutTimeExcds++;
  253.         break;
  254.     case ICMP_QUENCH:
  255.         icmpOutSrcQuenchs++;
  256.         break;
  257.     }
  258.     icmpOutMsgs++;
  259.     /* Now stick on the ICMP header */
  260.     if((bp = htonicmp(&icmp,data)) == NULLBUF){
  261.         free_p(data);
  262.         return -1;
  263.     }
  264.     return ip_send(INADDR_ANY,ip->source,ICMP_PTCL,ip->tos,0,bp,length,0,0);
  265. }
  266.